<template>
  <el-row :gutter="20" class="overview-row">
    <el-col :span="6" v-for="(item, index) in overviewStats" :key="index">
      <el-card class="overview-card" @click="handleCardClick(item)">
        <div class="overview-content">
          <div class="overview-icon" :style="{ backgroundColor: item.color }">
            <el-icon :size="24">
              <component :is="getIcon(item.icon)" />
            </el-icon>
          </div>
          <div class="overview-info">
            <div class="overview-title">{{ item.name }}</div>
            <div class="overview-number">{{ item.value }}</div>
            <div class="overview-trend" v-if="item.trend">
              <el-icon :size="12" :class="getTrendClass(item.trend.type)">
                <ArrowUp v-if="item.trend.type === 'up'" />
                <ArrowDown v-if="item.trend.type === 'down'" />
                <Minus v-if="item.trend.type === 'stable'" />
              </el-icon>
              <span>{{ item.trend.value }}%</span>
            </div>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
  import {
    ArrowUp,
    ArrowDown,
    Minus,
    Money,
    DocumentChecked,
    DocumentAdd,
    User,
  } from '@element-plus/icons-vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'

  interface OverviewItem {
    name: string
    value: number
    icon: string
    color: string
    trend?: {
      type: 'up' | 'down' | 'stable'
      value: number
    }
  }

  interface Props {
    overviewStats: OverviewItem[]
  }

  const props = defineProps<Props>()
  const router = useRouter()

  // 获取图标组件
  const getIcon = (iconName: string) => {
    const iconMap: Record<string, any> = {
      user: User,
      'pay-circle': Money, // 使用 Money 图标替代不存在的 PayCircle 图标
      'file-done': DocumentChecked, // 使用 DocumentChecked 图标替代不存在的 FileDone 图标
      'file-add': DocumentAdd, // 使用 DocumentAdd 图标替代不存在的 FileAdd 图标
    }
    return iconMap[iconName] || User
  }

  // 获取趋势样式类
  const getTrendClass = (type: string) => {
    return {
      'trend-up': type === 'up',
      'trend-down': type === 'down',
      'trend-stable': type === 'stable',
    }
  }

  // 处理卡片点击
  const handleCardClick = (item: OverviewItem) => {
    // 根据不同的统计类型跳转到对应页面
    if (item.name === '客户总数') {
      goToPage('/customer/index')
    } else if (item.name === '合同总数') {
      goToPage('/contract/index')
    } else if (item.name === '案件总数') {
      goToPage('/lawcase/index')
    }
  }

  // 跳转到指定页面
  const goToPage = (url: string) => {
    if (url) {
      router.push(url).catch(err => {
        ElMessage.error('页面跳转失败')
        console.error('页面跳转失败:', err)
      })
    }
  }
</script>

<style scoped>
  /* 概览统计样式 */
  .overview-row {
    margin-bottom: 24px;
  }

  .overview-card {
    height: 140px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }

  .overview-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  }

  .overview-content {
    display: flex;
    align-items: center;
    padding: 20px;
    height: 100%;
  }

  .overview-icon {
    width: 60px;
    height: 60px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 16px;
    color: white;
  }

  .overview-info {
    flex: 1;
  }

  .overview-title {
    font-size: 14px;
    color: #666;
    margin-bottom: 8px;
    font-weight: 500;
  }

  .overview-number {
    font-size: 28px;
    font-weight: bold;
    color: #333;
    margin-bottom: 4px;
  }

  .overview-trend {
    display: flex;
    align-items: center;
    font-size: 12px;
    color: #999;
  }

  .trend-up {
    color: #52c41a;
  }

  .trend-down {
    color: #f5222d;
  }

  .trend-stable {
    color: #999;
  }

  /* 响应式设计 */
  @media (max-width: 1200px) {
    .overview-row .el-col {
      margin-bottom: 16px;
    }
  }

  @media (max-width: 768px) {
    .overview-card {
      height: 120px;
    }

    .overview-content {
      padding: 16px;
    }

    .overview-icon {
      width: 48px;
      height: 48px;
    }

    .overview-number {
      font-size: 24px;
    }
  }
</style>
